<?php

namespace App\Admin\Controllers;

use App\Models\Area;
use App\Models\Complaint;
use DB;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class ComplaintController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('旅游投诉');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Complaint::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableExport();
            $grid->model()->where('status', 0);
            $grid->id('ID')->sortable();
            $grid->name('投诉人');
            $grid->behavior('投诉行为')->using(config('options.complaint_behavior'));
            $grid->phone('联系电话');
            $grid->status('状态')->radio(config('options.complaint_status'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Complaint::class, function (Form $form) {
            $form->tab('投诉信息',function ($form){
                $form->select('area_id','区域')->options(Area::all()->pluck('title','id'));
                $form->select('type','事件类型')->options(config('options.complaint_type'));
                $form->select('target','投诉类型')->options(config('options.credit_module'))->load('target_id', '/api/target_id');
                $form->select('target_id','投诉对象');
                $form->select('behavior','投诉行为')->options(config('options.complaint_behavior'));
                $form->text('other','其他')->help('投诉类型为其他时选填此项');
                $form->text('name','投诉人');
                $form->text('phone','联系电话');
                $form->textarea('cause','投诉事由/诉求');
                $form->date('time','投诉日期');
            });
            $form->hidden('status','状态')->default(0);
        });
    }
}
